$(document).ready(function(){
	var path = $("#path").val();	
	   //更改考试状态	
		$(".subject-status").change(function(){			
			$.ajax({
				type:'POST',
				url:path+'/changeStatus',
				data:{
					sub_id : this.id
				},
				dataTyp : "json",
				success:function(data){
					if(data){
						alert("状态更改成功！");
					}else{
						alert("状态更改失败！");
					}
													
				}
			});
		 
		}); 
		//管理参考学生
		$(".manage-subject-student-btn").click(function(){
			var sub_id = $(this).parents("td").siblings("td").eq(1).text();
			
			$.ajax({
				type : 'POST',
				url : path+'/getAllStudent',
				data : {
					sub_id : sub_id 
					},
				dataType : "json",
				success : function(data){
					var htmlString = '';
					var selectedUser = data.selectedUser;
					//得到所有这个科目关联的学生
					for(var i = 0; i < data.allUser.length; i++){
						var user = data.allUser[i];
						var checked = checkSelectedUser(selectedUser ,user.user_id) ? 'checked="checked"' : '';
						htmlString +='<div class="checkbox">';
						htmlString +=' <label>';
						htmlString +='   <input name="subjectStudents" class="ace ace-checkbox-2" type="checkbox"  value="'
							              +user.user_id + ' " '+checked + ' />';
						htmlString += '  <span class="lbl">'+user.user_name + '</span>';
						htmlString += '  </label>';
						htmlString += ' </div>';
					}
					//把所有学生加入到弹出框subjectStudentModal里边的内容区
					$("#subjectStudentContent").html(htmlString);
					$("#subjectId").val(sub_id);
				}
			});
			$("#modalSubjectName").text($(this).parents("td").siblings("td").eq(2).text());
			$("#subjectStudentModal").modal("show");
		});
		//修改参考学生的勾选后，点击保存按钮
		$("#subjectStudentSaveBtn").on('click',function(){
			var subjectStudents = [];
			//获取所有当前已经勾选的学生学号
			alert("我已经进来了");
			for(var i = 0; i < $('input[name="subjectStudents"]:checked').length ; i++){
				subjectStudents[i] = $($('input[name="subjectStudents"]:checked')[i]).val();				
			}
			
			$.ajax({
				type : 'POST',
				url : path+"/saveSubjectStudents",
				data : {
					subjectStudents :subjectStudents,//所有已勾选的参考学生
					sub_id : $("#subjectId").val() //参考学生对应的参考科目
				},
				dataType : "json",
				success : function(data){
					if(data.success){
						alert('保存成功');
						$("#subjectStudentModal").modal("hide");
					}else{
						alert('保存失败');
					}
				}
			});
		});
		
});
function checkSelectedUser(selectedUser, user_id){
	var flag = false;
	for(var i = 0;i < selectedUser.length ; i++){		
		if(selectedUser[i] == user_id){
			flag = true;
		}
	}
	return flag;
}